package com.example.vo

import android.content.Intent
import android.content.res.AssetManager
import android.os.Bundle
import android.os.Handler
import com.example.vo.Controller.ActivityCollector.finishAll
import com.example.vo.Controller.BaseActivity
import com.example.vo.Data.DB
import com.example.vo.Data.DB_History
import java.io.FileOutputStream
import java.io.InputStream

class StartActivity : BaseActivity(){
    private val SPLASH_DISPLAY_LENGHT = 2000 //延迟2秒

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start)
        Handler().postDelayed(Runnable(){
            val dbhelper = DB(this, "AllWord.db", 1)
            val dbhelper2 = DB(this, "AllTWord.db", 1)
            val dbhelper3 = DB_History(this, "History.db",1)
//            val dbhelper4 = DB(this,"STWord.db",1)
            dbhelper.writableDatabase
            dbhelper2.writableDatabase
            dbhelper3.writableDatabase
//            dbhelper4.writableDatabase
            writeWord()
            writeTWord()
            writesword()
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finishAll()
        }, SPLASH_DISPLAY_LENGHT.toLong())
    }

    private fun writeWord(){
        val filepath = "data/data/com.example.vo/databases/AllWord.db"
        try {
            val out = FileOutputStream(filepath)
            val assetManager: AssetManager = this.assets
            val ins: InputStream = assetManager.open("AllWord.db")
            val buffer = ByteArray(1024)
            var count = ins.read(buffer)
            while (count > 0){
                out.write(buffer, 0, count)
                count = ins.read(buffer)
            }
            ins.close()
            out.close()
        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    private fun writeTWord(){
        val filepath = "data/data/com.example.vo/databases/AllTWord.db"
        try {
            val out = FileOutputStream(filepath)
            val assetManager: AssetManager = this.assets
            val ins: InputStream = assetManager.open("AllTWord.db")
            val buffer = ByteArray(1024)
            var count = ins.read(buffer)
            while (count > 0){
                out.write(buffer, 0, count)
                count = ins.read(buffer)
            }
            ins.close()
            out.close()
        }catch (e: Exception){
            e.printStackTrace()
        }
    }

    private fun writesword(){

    }
}